package com.pro.service.imple;

import com.pro.common.GuavaCommon;
import com.pro.dao.ProductMapper;
import com.pro.entity.*;
import com.pro.service.ProductService;
import com.pro.utils.GuavaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

@Service
public class ProductServiceImaple implements ProductService{
    @Autowired
    private ProductMapper productMapper;

    /* 刘弘禹
     * 2021/11/3
     * 查询商品
     * */
    @Override
    public List<Product> queryProduct(Product product) {
        List<Product> products = productMapper.queryProduct(product);
        if(product.getProductId() != null){
            Integer collection = productMapper.getCollection(3, product.getProductId());
            Integer salesVolume = productMapper.getSalesVolume(product.getProductId(), 6, 3);
            products.get(0).setCollectionVolume(collection);
            products.get(0).setSalesVolume(salesVolume);
        }
        return products;
    }

    /* 刘弘禹
     * 2021/11/3
     * 查询商品分类
     * */
    @Override
    public List<ProductCategroy> productClass() {
        return productMapper.productClass();
    }

    /* 刘弘禹
     * 2021/11/4
     * 修改上架下架状态
     * */
    @Override
    public Boolean updateState(Integer state,Integer id) {
        Boolean aBoolean = productMapper.updateState(state,id);
        return aBoolean;
    }
    //修改商品
    @Override
    public Boolean updateProduct(Product product) {
        Boolean aBoolean = productMapper.updateProduct(product);
        return aBoolean;
    }
    //添加商品价格修改记录
    @Override
    public Boolean addProductJournal(NavigationBarData navigationBarData) {
        Boolean aBoolean = productMapper.addProductJournal(navigationBarData);
        return aBoolean;
    }

    /*
    添加事务
    添加商品*/
    @Override
    @Transactional
    public Boolean addProduct(Product product) {
        product.setOriginalpriceProduct(0.0);
        product.setHits(0);
        product.setProductSate(2);
        product.setCreateDate(new Date());
        Boolean aBoolean = productMapper.addProduct(product);
        CourseRecord courseRecord = new CourseRecord(null,product.getProductId(),product.getCreateDate(),
                product.getCreateId(),null,null,2,null,3);
        Boolean aBoolean1 = addProductCourse(courseRecord);
        if(aBoolean && aBoolean1){
            return true;
        }
        return false;
    }

    //添加商品审核记录
    @Override
    public Boolean addProductCourse(CourseRecord courseRecord) {
        Boolean aBoolean = productMapper.addProductCourse(courseRecord);
        return aBoolean;
    }

    //删除商品
    public Boolean delProduct(Integer productId){
        Boolean aBoolean = productMapper.delProduct(productId);
        return aBoolean;
    }

    @Override
    public Boolean delProductCaegory(Integer categoryId) {
        Boolean aBoolean = productMapper.delProductCaegory(categoryId);
        return aBoolean;
    }

    @Override
    public Boolean updateProductCaegory(Integer categoryId, String title) {
        Boolean aBoolean = productMapper.updateProductCaegory(categoryId, title);
        return aBoolean;
    }

    @Override
    public Boolean addProductCaegory(ProductCategroy productCategroy) {
        Boolean aBoolean = productMapper.addProductCaegory(productCategroy);
        return aBoolean;
    }

    @Override
    public List<NavigationBarData> getNavIgation(NavigationBarData navigationBarData) {
        List<NavigationBarData> navIgation = productMapper.getNavIgation(navigationBarData);
        return navIgation;
    }

    @Override
    public List<CourseRecordPlus> queryCourseRecord(Integer id) {
        List<CourseRecordPlus> courseRecords = productMapper.queryCourseRecord(id);
        return courseRecords;
    }

    @Override
    public Boolean addProductOperationRecord(JsLog dictData) {
        Boolean aBoolean = productMapper.addProductOperationRecord(dictData);
        return aBoolean;
    }

    @Override
    @Transactional
    public Boolean addExcel(Product product) {
        Boolean aBoolean = productMapper.addProduct(product);
        if (aBoolean){
            CourseRecord courseRecord = new CourseRecord(null,product.getProductId(),new Date(),
                    product.getCreateId(),null,null,2,null,3);
            Boolean aBoolean1 = addProductCourse(courseRecord);
            if (aBoolean1){
                return true;
            }
        }
        return false;
    }
}
